<?php
declare (strict_types = 1);
namespace app\controller;

use app\BaseController;
use think\App;

class Base extends BaseController
{
    // 逻辑层
    protected $logic = null;

    protected function jsonReturn($code, $msg = "error", $data = [], $url = "", $httpCode = 200)
    {
        if(is_array($code)) {
            // 定义数组下标名
            $sub_name = ['code', 'msg', 'data', 'url', 'httpCode'];
            // 删除多余下标
            array_splice($sub_name, count($code));
            // 合并新数组 ['code' => xxx, ...]
            $merge_arr = array_combine($sub_name, $code);
            // 取出数组值赋值给键名且作为变量
            extract($merge_arr);
        } else {
            $code = isset($code) ? (int) $code : config('status.fail');
        }

        $result = [
            'code'  => $code,
            'msg'   => $msg,
        ];

        // 以下参数为空则不传
        !empty($data) && $result['data'] = $data;
        !empty($url) && $result['url'] = $url;

        return json($result, $httpCode);
    }
}